<template>
  <div>
    <el-form ref="loginForm" :rules="loginRules" :model="loginForm" class="login-form" label-width="0">
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          :placeholder="$t('login.username')"
          size="small"
          auto-complete="off"
          @keyup.enter.native="handleLogin"
        >
          <template slot="prepend">用户名&nbsp;</template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          :type="passwordType"
          :placeholder="$t('login.password')"
          size="small"
          auto-complete="off"
          @keyup.enter.native="handleLogin"
        >
          <template slot="prepend">&nbsp;&nbsp;密码&nbsp;&nbsp;</template>
          <i v-if="passwordType" slot="suffix" class="psw-icon-close el-input__icon" @click="showPassword" />
          <i v-else slot="suffix" class="psw-icon-open el-input__icon" @click="showPassword" />
        </el-input>
      </el-form-item>
      <el-form-item v-if="website.captchaMode" prop="code">
        <el-row :span="24">
          <el-col :span="11">
            <el-input
              v-model="loginForm.code"
              class="w140"
              :placeholder="$t('login.code')"
              size="small"
              auto-complete="off"
              @keyup.enter.native="handleLogin"
            >
              <template slot="prepend">验证码&nbsp;</template>
            </el-input>
          </el-col>
          <el-col :span="7">
            <div class="login-code">
              <img :src="loginForm.image" class="login-code-img" @click="refreshCode">
            </div>
          </el-col>
        </el-row>
      </el-form-item>

      <div class="errorContainer">
        <div v-if="showError" class="errorBox">
          <img class="errorImg" src="/img/bg/notice.png">
          <span class="errorText"> {{ errorMessage }} </span>
        </div>
      </div>

    </el-form>
    <el-button type="primary" size="small" class="login-submit defulat-btn" @click.native.prevent="handleLogin">{{
      $t('login.submit')
    }}
    </el-button>
  </div>
</template>

<script>

import loginMixins from './login'
export default {
  name: 'Userlogin',
  mixins: [loginMixins],
  data() {
    return {
      loginForm: {
        // 用户名
        username: '',
        // 密码
        password: '',
        // 账户类型
        type: 'account',
        // 验证码的值
        code: '',
        // 验证码的索引
        key: '',
        // 预加载白色背景
        image: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, message: '密码长度最少为6位', trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      // 登录错误
      showError: false,
      errorMessage: '登录账户或密码错误，请重新输入'
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    showPassword() {
      this.passwordType === ''
        ? (this.passwordType = 'password')
        : (this.passwordType = '')
    },
    befroeHandleLogin() {
      return Promise.resolve(this.loginForm)
    }
  }
}
</script>

<style lang="scss">
.psw-icon-open,
.psw-icon-close {
  display: inline-block;
  width: 20px;
  height: 20px;
  position: relative;
  top: 15px;
}

.psw-icon-open {
  background: url("/img/bg/psw-open.png") no-repeat;
}
.psw-icon-close {
  background: url("/img/bg/psw-close.png") no-repeat;
}
.errorContainer {
  color: #FF552E;
  font-size: 14px;

  .errorBox {
    display: flex;
    line-height: 24px;
  }

  .errorImg {
    position: relative;
    top: 0;
    margin-right: 5px;
  }
}
</style>
